# coding=utf-8
import os
import sys
import types
import base64
import platform
import ConfigParser

# 定义根目录
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
WEB_ROOT = os.path.join(BASE_DIR, 'xls_demo')
XLS_DEMO_BASE_PATH = os.environ.get('XLS_DEMO_BASE_PATH')
sys.path.append(WEB_ROOT)

# 数据库定义
XLS_DEMO_DB_HOST = os.environ.get('XLS_DEMO_DB_HOST', '127.0.0.1')
XLS_DEMO_DB_NAME = os.environ.get('XLS_DEMO_DB_NAME', 'xls_demo')
XLS_DEMO_DB_PASSWORD = str(os.environ.get('XLS_DEMO_DB_PASSWORD', ''))
XLS_DEMO_DB_PORT = str(os.environ.get('XLS_DEMO_DB_PORT', '3306'))
XLS_DEMO_DB_USER = str(os.environ.get('XLS_DEMO_DB_USER', 'root'))

# DEBUG
XLS_DEMO_HTTP_DEBUG = int(os.environ.get('XLS_DEMO_HTTP_DEBUG', '0'))
XLS_DEMO_SETTINGS_FILE = os.environ.get('XLS_DEMO_SETTINGS_FILE')


platform_system = platform.system()
parser = ConfigParser.SafeConfigParser()
parser.readfp(open(XLS_DEMO_SETTINGS_FILE))

# 读配置文件
# try:
#     XLS_DEMO_SYNC_LOG_COUNT = parser.getint('server', 'sync_logs_unit')
# except:
#     XLS_DEMO_SYNC_LOG_COUNT = 300
# try:
#     XLS_DEMO_DB_AUTO_OPTIMIZE = parser.getboolean('db', 'auto_optimize')
# except:
#     XLS_DEMO_DB_AUTO_OPTIMIZE = False
# try:
#     XLS_DEMO_DB_AUTO_OPTIMIZE_PERIOD = parser.getfloat('db', 'auto_optimize_days')
# except:
#     XLS_DEMO_DB_AUTO_OPTIMIZE_PERIOD = 7.0
# try:
#     XLS_DEMO_NTP_CRON = parser.getboolean('server', 'ntp_cron')
# except:
#     XLS_DEMO_NTP_CRON = True


TMP_ROOT = os.path.join(WEB_ROOT, 'tmp')


# 生成临时文件夹tmp/*
if not os.path.exists(TMP_ROOT):
    os.mkdir(TMP_ROOT)
CACHE_TMP_ROOT = os.path.join(TMP_ROOT, 'cache')
if not os.path.exists(CACHE_TMP_ROOT):
    os.mkdir(CACHE_TMP_ROOT)
# LOG_PATH = os.path.join(BASE_PATH, 'logs')
# if not os.path.exists(LOG_PATH):
#     os.mkdir(LOG_PATH)
# UPLOAD_TMP_ROOT = os.path.join(TMP_ROOT, 'upload')
# if not os.path.exists(UPLOAD_TMP_ROOT):
#     os.mkdir(UPLOAD_TMP_ROOT)

    
# 调试模式
if XLS_DEMO_HTTP_DEBUG == 0:
    DEBUG = False
else:
    DEBUG = True

TEMPLATE_DEBUG = DEBUG

ALLOWED_HOSTS = ['*']

SECRET_KEY = '!3d29qp)_mmxjqwh(o5)2t5v(t+4xt0nwo@hgfm!)4+oa8+rgg'

ADMINS = (
    ('admin', 'admin@admin.com'),
)




# # 定义缓存
# if platform_system == 'Linux':
#     CACHES = {
#         'default': {
#             'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
#         }
#     }
# elif platform_system == 'Windows':
#     CACHES = {
#         'default': {
#             'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
#             'LOCATION': '127.0.0.1:11211',
#         }
#     }



# app定义
INSTALLED_APPS = (
    'django.contrib.sessions',
    'django.contrib.staticfiles',
    
    'xls_demo.db',
)


# 中间件
MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'xls_demo.utils.middleware.JsonResponseMiddleware',
)


# # 自定义User
# AUTH_USER_MODEL = 'db.CustomUser'




# URL配置文件
ROOT_URLCONF = 'xls_demo.urls'

WSGI_APPLICATION = 'xls_demo.wsgi.application'


# 数据库配置
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'CONN_MAX_AGE': 3600,
        'NAME': XLS_DEMO_DB_NAME,
        'USER': XLS_DEMO_DB_USER,
        'PASSWORD': XLS_DEMO_DB_PASSWORD,
        'HOST': XLS_DEMO_DB_HOST,
        'PORT': XLS_DEMO_DB_PORT,
#        #test database support utf8
#        'TEST_CHARSET': "utf8",
#        'TEST_COLLATION': "utf8_general_ci",
    }
}

# # test use sqlite3
# import sys
# if 'test' in sys.argv:
#     DATABASES['default'] = {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': 'memory',
#     }



# 语言时区国际化定义
LANGUAGE_CODE = 'zh-cn'
LANGUAGES = ('zh', 'Chinese')

# TIME_ZONE = 'UTC'
# USE_I18N = True
# USE_L10N = True
# USE_TZ = True

TIME_ZONE = 'Asia/Shanghai'
USE_I18N = False
USE_L10N = True
USE_TZ = False



# 静态文件URL
STATIC_ROOT = ''

STATIC_URL = '/static/'

STATICFILES_DIRS = ('',
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.    
    # MH:required!
    os.path.join(WEB_ROOT, 'public').replace('\\','/'),

    ('css', '/public/css'),
    ('images', '/public/images'),
    ('js', '/public/js'),
    ('downloads', '/public/downloads'),# ('downloads', '/static/downloads'), also OK
)

STATICFILES_MIRROR = os.path.join(WEB_ROOT, 'public').replace('\\','/')

TEMPLATE_DIRS = ('',
    os.path.join(WEB_ROOT, 'templates').replace('\\','/'),
    'django.template.loaders.filesystem.Loader',
)


TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)




# os_environ = os.environ
# objs = dir()
# f = open(os.path.join(LOG_PATH, 'settings.log'), 'w')
# b = bytearray()
# for var in objs:
#     if var.startswith('_'):
#         continue
#     if isinstance(eval(var), types.ModuleType):
#         continue
#     b += bytearray(var + ': ')
#     b += bytearray(str(eval(var)))
#     b += bytearray('\n')
# s = base64.b64encode(b)
# f.write(s)
# f.close()


